package com.woniu.secondhand.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.common.entity.ResultCode;
import com.woniu.common.exception.LifeException;
import com.woniu.secondhand.mapper.SecondhandAccessMapper;
import com.woniu.secondhand.pojo.SecondhandAccess;
import com.woniu.secondhand.service.ISecondhandAccessService;
import com.woniu.user.vo.UserLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author HMMT
 * @since 2023-05-22
 */
@Service
public class SecondhandAccessServiceImpl extends ServiceImpl<SecondhandAccessMapper, SecondhandAccess> implements ISecondhandAccessService {
    @Autowired
    private SecondhandAccessMapper accessMapper;

    @Override
    public int addAccess(Integer productId, UserLoginVO userLoginVO) {
        SecondhandAccess access = new SecondhandAccess();
        QueryWrapper<SecondhandAccess> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userLoginVO.getId());
        wrapper.eq("product_id", productId);
        access = accessMapper.selectOne(wrapper);
        if (access != null) {
            throw new LifeException(ResultCode.DATA_ALREADY_EXISTED.getMessage(), ResultCode.DATA_ALREADY_EXISTED);
        }
        access = new SecondhandAccess();
        access.setUserId(userLoginVO.getId());
        access.setProductId(productId);
        int insert = accessMapper.insert(access);
        return insert;
    }
}
